﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using Pharmacy.Data;
using Pharmacy.Settings.BusinessLayer;
using Microsoft.Reporting.WebForms;
using Pharmacy.Entities;

public partial class Admin_PreviewVoid : System.Web.UI.Page
{
    DateTime tmpfromdate, tmptodate, fromdate, todate;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                if (Request.QueryString["From"] != null && Request.QueryString["To"] != null)
                {
                    tmpfromdate = DateTime.Parse(Request.QueryString["From"].ToString());
                    tmptodate = DateTime.Parse(Request.QueryString["To"].ToString());
                    fromdate = new DateTime(tmpfromdate.Year, tmpfromdate.Month, tmpfromdate.Day, 0, 0, 0);
                    todate = new DateTime(tmptodate.Year, tmptodate.Month, tmptodate.Day, 23, 59, 59);
                    PreviewVoid(fromdate, todate);

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
    private void PreviewVoid(DateTime from, DateTime to)
    {
        DataSet ds = new DataSet();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandTimeout = 0;
        cmd.CommandText = "_PrescriptionVoid_GetByDate";
        cmd.Parameters.Add("@fromdate", SqlDbType.DateTime).Value = from;
        cmd.Parameters.Add("@todate", SqlDbType.DateTime).Value = to;
        ds = DataRepository.Provider.ExecuteDataSet(cmd);
        DataTable ntable = ds.Tables[0];

        DSVoid.PrevoidDataTable dtPrevoid = new DSVoid.PrevoidDataTable();
        DSVoid.PrevoidRow dtPrevoidRow;
        for (int i = 0; i < ntable.Rows.Count; i++)
        {
            dtPrevoidRow = dtPrevoid.NewPrevoidRow();
            DataRow drw = ntable.Rows[i];
            dtPrevoidRow.PrescriptionID = drw["PrescriptionID"].ToString();
            dtPrevoidRow.VoidDate = DateTime.Parse(drw["VoidDate"].ToString());
            dtPrevoidRow.VoidReason = drw["VoidReason"].ToString();
            dtPrevoidRow.VoidPerson = drw["VoidPerson"].ToString();
            dtPrevoidRow.TotalAmount = decimal.Parse(drw["TotalAmount"].ToString());
            dtPrevoid.AddPrevoidRow(dtPrevoidRow);
        }
        //title
        DSVoid.TitleDataTable dtTitle = new DSVoid.TitleDataTable();
        DSVoid.TitleRow dtTitleRow = dtTitle.NewTitleRow();
        dtTitleRow.BrandName = ServiceFacade.SettingsHelper.DnBrandName;
        dtTitleRow.CompanyAddress = ServiceFacade.SettingsHelper.DnCompanyAddress;
        //get prepareby from cookies
        var httpCookie = Request.Cookies["LoginUser"];
        WebUser id = DataRepository.WebUserProvider.GetByUserId(httpCookie.Value);
        dtTitleRow.PrepareBy = id.Name;
        dtTitle.AddTitleRow(dtTitleRow);

        ReportViewer1.LocalReport.EnableExternalImages = true;
        ReportViewer1.LocalReport.DataSources.Clear();
        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DSVoid_Prevoid", dtPrevoid));
        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DSVoid_Title", dtTitle));
        ReportViewer1.DataBind();
    }
}
